package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.BatchOrder;
import cn.stylefeng.guns.modular.business.entity.Order;
import cn.stylefeng.guns.modular.business.model.params.BatchOrderParam;
import cn.stylefeng.guns.modular.business.model.result.BatchNoAndPwdResult;
import cn.stylefeng.guns.modular.business.model.result.BatchOrderResult;
import cn.stylefeng.guns.modular.business.service.BatchOrderService;
import cn.stylefeng.guns.modular.business.service.OrderService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 批次订单控制器
 *
 * @author jwj
 * @Date 2019-09-09 18:08:55
 */
@Controller
@RequestMapping("/batchOrder")
public class BatchOrderController extends BaseController {

    private String PREFIX = "/business/batchOrder";

    @Autowired
    private BatchOrderService batchOrderService;

    @Autowired
    private OrderService orderService;

    /**
     * 跳转到主页面
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/batchOrder.html";
    }

    /**
     * 新增页面
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/batchOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam Integer batchId, Model model) {
        BatchOrder batchOrder = batchOrderService.getById(batchId);
        if (batchOrder != null) {
            BatchOrderResult result = new BatchOrderResult();
            ToolUtil.copyProperties(batchOrder, result);
            result.setCardNOs(JSONArray.parseArray(batchOrder.getCardNoPwd(), BatchNoAndPwdResult.class));
            List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("batch_order_id", batchId).eq("delete_flag", RecoverConst.DEL_FLAG_FALSE));
            result.setOrderList(orderList);
            model.addAttribute("batchOrder", result);
        }
        return PREFIX + "/batchOrder_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BatchOrderParam batchOrderParam) {
        this.batchOrderService.add(batchOrderParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BatchOrderParam batchOrderParam) {
        this.batchOrderService.update(batchOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BatchOrderParam batchOrderParam) {
        this.batchOrderService.delete(batchOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BatchOrderParam batchOrderParam) {
        BatchOrder detail = this.batchOrderService.getById(batchOrderParam.getBatchId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jwj
     * @Date 2019-09-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BatchOrderParam batchOrderParam) {
        return this.batchOrderService.findPageBySpec(batchOrderParam);
    }

}


